﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TuongLaiDangCho.DTO;
using TuongLaiDangCho.DAO;

namespace TuongLaiDangCho
{
    public partial class ucDanhNgon : DevExpress.XtraEditors.XtraUserControl
    {
        DanhNgonDTO danhngon = new DanhNgonDTO();
        public ucDanhNgon()
        {
            InitializeComponent();
            danhngon.Date = frmHome.choiseDate;
            grovDanhNgon.Text = DataProvider.Today(frmHome.choiseDate) + " " + frmHome.choiseDate.ToShortDateString();
        }
        
        private void LoadDanhNgon()
        {
            grcDanhNgon.DataSource = null;
            grcDanhNgon.DataSource = DanhNgonDAO.LoadDanhNgon();
        }

        private void grcDanhNgon_Load(object sender, EventArgs e)
        {
            LoadDanhNgon();
            frmHome.maxindex = DanhNgonDAO.LoadDanhNgon().Rows.Count;
        }

        private void grv_DanhNgon_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            string s_Error = "";
            bool bVali = true;
            if (grv_DanhNgon.GetRowCellValue(e.RowHandle, "NoiDung").ToString() == "" || grv_DanhNgon.GetRowCellValue(e.RowHandle, "TacGia").ToString() == "")
            {
                bVali = false;
                s_Error = "Không thể để trống mục này";
                LoadDanhNgon();
            }

            if (bVali)
            {
                if (e.RowHandle < 0)
                {
                    danhngon.NoiDung = grv_DanhNgon.GetRowCellValue(e.RowHandle, "NoiDung").ToString();
                    danhngon.TacGia = grv_DanhNgon.GetRowCellValue(e.RowHandle, "TacGia").ToString();
                    danhngon.Date = DateTime.Today;
                    DanhNgonDAO.InsertDanhNgon(danhngon);
                    LoadDanhNgon();
                    frmHome.maxindex = DanhNgonDAO.LoadDanhNgon().Rows.Count;
                }
                else
                {
                    danhngon.NoiDung = grv_DanhNgon.GetRowCellValue(e.RowHandle, "NoiDung").ToString();
                    danhngon.TacGia =grv_DanhNgon.GetRowCellValue(e.RowHandle, "TacGia").ToString();
                    danhngon.Date=(DateTime)(grv_DanhNgon.GetRowCellValue(e.RowHandle, "NgayDang"));
                    danhngon.Id = int.Parse(grv_DanhNgon.GetRowCellValue(e.RowHandle, "ID").ToString());
                    //XtraMessageBox.Show(grv_DanhNgon.GetRowCellValue(e.RowHandle, "NoiDung").ToString() + "\n" +
                        //grv_DanhNgon.GetRowCellValue(e.RowHandle, "SoTien").ToString() + "\n" +
                        //grv_Expenditure.GetRowCellValue(e.RowHandle, "ID").ToString() + "\n");
                    DanhNgonDAO.UpdateDanhNgon(danhngon);
                    //LoadExpen();
                }
            }
            else
            {
                bVali = false;
                XtraMessageBox.Show(s_Error);
            }
        }

        private void grv_DanhNgon_InvalidRowException(object sender, DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e)
        {
            e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
        }

        private void grcDanhNgon_ProcessGridKey(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete && grv_DanhNgon.State != DevExpress.XtraGrid.Views.Grid.GridState.Editing)
            {
                if (XtraMessageBox.Show("Are You Sure?", "Quets", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    danhngon.Id = int.Parse(grv_DanhNgon.GetRowCellValue(grv_DanhNgon.FocusedRowHandle, "ID").ToString());
                    DanhNgonDAO.DeleteDanhNgon(danhngon);
                    LoadDanhNgon();
                }
            }
        }

       
    }
}
